<template>
  <el-dialog
    :title="view === 'create'? '添加物流信息': '修改物流信息'"
    :visible="true"
    custom-class="dialogWrapper"
    :before-close="closeDialog"
    >
    <el-form :model="formData" label-width="80px" ref="ruleForm" :rules="rules">
     <el-form-item label="物流名称" prop="logisticsName" required>
        <el-input v-model.trim="formData.logisticsName" auto-complete="off"></el-input>
     </el-form-item>
     <el-form-item label="物流编号" prop="logisticsCode" required>
        <el-input v-model.trim="formData.logisticsCode" auto-complete="off"></el-input>
     </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submit('ruleForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { create, edit } from "@/services/order";
import { RequestError } from "@/services/errors";
import { mapActions } from "vuex";
// import {login} from '@/services/login'
// import menuModules from '@/router/modules.json'
var vailNumber = (rule, value, callback) => {
	var NumberyRegex = /^[0-9]*$/;
	if (!NumberyRegex.test(value)) {
	    callback(new Error('格式不正确,只能输入数字'))
	    } else {
	        callback();
	    }
};
export default {
  name: "edit-role",
  data () {
    return {
      menus: [],
      list: [],
      orderNo:"",
      formData: {
        logisticsName: "",
        logisticsCode: ""
      },
      menuInfo: [],
      dialogFormVisible: false,
      rules: {
        logisticsName: [{ required: true, message: "请输入物流名称", trigger: "blur" },
        { min: 1, max: 50, message: '长度在 50个字符以内', trigger: 'blur' }],
        logisticsCode: [{ required: true, message: "请输入物流编号", trigger: "blur" },
        { min: 1, max: 12, message: '长度在 12个字符以内', trigger: 'blur' },
        { validator: vailNumber, trigger: 'blur'}]
      }
    }
  },
  // data: () => ({
  //   menus: [],
  //   list: [],
  //   formData: {
  //     roleName: "",
  //     roleType: "",
  //     roleDesc: ""
  //   },
  //   menuInfo: [],
  //   dialogFormVisible: false,
  //   rules: {
  //     roleName: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
  //     roleType: [{ required: true, message: "请输入角色类型", trigger: "blur" }],
  //     roleDesc: [{ required: true, message: "请输入角色描述", trigger: "blur" }]
  //   }
  // }
  // ),
  props: {
    view: {
      type: String,
      default: "create"
    }
  },
  methods: {
    ...mapActions(["setUserInfo"]),
    closeDialog() {
      this.$emit("close");
    },
    submit(formName) {
    	console.log(this.formData)
      this.$refs[formName].validate(async valid => {
        if (valid) {
          try {
            if (this.view === "create") {
              await create({
                orderNo: this.orderNo,
                logisticsName: this.formData.logisticsName,
                logisticsCode: this.formData.logisticsCode,
              });
            } else {
              await edit({
                id: this.formData.id,
                orderNo: this.formData.orderNo,
                logisticsName: this.formData.logisticsName,
                logisticsCode: this.formData.logisticsCode
              });
            }
            this.dialogFormVisible = false;
            this.$emit("close", true);
            this.$message({
              showClose: true,
              message: "提交成功",
              type: "success"
            });
          } catch (e) {
            // this.$alert(e.message, "提示", {
            //   type: "error",
            //   confirmButtonText: "确定"
            // });
            if (e instanceof RequestError) {
              this.$alert(e.message, "提示", {
                type: "error",
                confirmButtonText: "确定"
              });
            } else {
              throw e;
            }
          }
        } else {
          return;
        }
      });
    }
  },
  mounted() {}
};
</script>
<style>
.dialogWrapper {
  width: 540px;
}
.el-select {
  width: 100%;
}
</style>
